<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/10/24
 * Time: 16:19
 */

namespace app\api\controller\other;


use app\api\controller\BasicApi;
use service\FileService;
use service\QiruismsService;
use think\Db;
use think\Exception;
use think\facade\Cache;

class System extends BasicApi
{
    /**
     * 获取自定义分类
     * @author jungshen
     * @param pid 上级ID 多个用,隔开
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_cate(){
        $pid=input('pid',0);
        $allow_push=input('allow_push',1);
        $db=Db::name('my_cate')->where('status',1)->where('allow_publish',$allow_push);
        if(is_numeric($pid)){
            $db->where('pid',$pid);
        }elseif(preg_match('/^[1-9]+,[1-9]+$/',$pid)){
            $db->whereIn('pid',$pid);
        }else{
            throw new Exception('参数有误');
        }
        $cates=$db->order('sort,id desc')->field('id,icon,title,content')->select();
        return json(['msg'=>'success','data'=>$cates]);
    }

    /**
     * 获取系统分类
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function cate(){
        $pid=input('pid',0);
        $allow_publish=input('allow_publish/d',1);
        $db=Db::name('system_cate')
            ->where('status',1)
            ->where('allow_publish',$allow_publish);
        if(is_numeric($pid)){
            $db->where('pid',$pid);
        }elseif(preg_match('/^[1-9]+,[1-9]+$/',$pid)){
            $db->whereIn('pid',$pid);
        }else{
            throw new Exception('参数有误');
        }
        $cates=$db->order('sort,id desc')->field('id,icon,title,desc,content')->select();
        return json(['msg'=>'success','data'=>$cates]);
    }


    /**
     * 获取首页banner
     * @author  jungshen
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function banner(){
        //查询广告
        $type=$this->request->get('type/d',17);
        $banner=Db::name('system_ad')->where('status',1)->where('type',$type)->order('sort,id desc')->field('image,url')->select();
        return json(['msg'=>'success','data'=>$banner]);
    }

    /**
     * 上传图片
     * @return \think\response\Json
     * @throws Exception
     * @throws \OSS\Core\OssException
     * @throws \think\exception\PDOException
     */
    public function upload_pic()
    {
        $file = $this->request->file('file');
        if (!$file->checkImg()) {
            return json(['msg' => '只能上传图片类型文件'],403);
        }
        if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
            return json(['msg' => '文件上传类型受限'],403);
        }
        // 文件上传处理
        if (($info = $file->move("static/upload"))) {
            $filename=$info->getSaveName();
            if ($url = FileService::getFileUrl($filename, 'local')) {
                $data['data']['url']=str_replace('\\','/',$url);
                $data['msg']='文件上传成功';
                return json($data);
            }
        }
        return json(['msg' => '文件上传失败'],500);
    }

}